/*  header-comment
/*  file   : /Users/arida/react-hrm-h5/src/actions/steps/getUser
/*  author : arida
/*  date   : 2017-6-4 18:27:55
/*  last   : 2017-8-4 17:38:17
*/
import { createAction } from "redux-actions";
import fetch from "../../utils/fetch.js";
import { get } from "../../utils/db";

export const getOptions = createAction("STEP_TWO_GET_OPTIONS");
export const getUser = createAction("STEP_TWO_GET_USER");

export const fetchDropDownOpthions = () => dispatch =>
	fetch(`${get("baseUrl")}getAllDropDownMap`, {
		meta: {
			dipId: 63663
		}
	}).then(res => {
		if (res.success) {
			dispatch(getOptions(res.result));
		}
		return res;
	});

export const fetchAllFields = userId => dispatch => {
	const url = `${get("baseUrl")}getEmployeeInfoV2`;
	return fetch(url, {
		meta: {
			dipId: 68170
		},
		body: userId
			? {
				userId
			}
			: undefined
	}).then(res => {
		if (res.success) {
			if (res.result && !res.result.empExistFlag) {
				window.dd.device.notification.alert({
					message: "员工已从待入职列表删除",
					buttonName: "我知道了",
					onSuccess: function () {
						window.dd.biz.navigation.close();
					}
				});
			} else {
				dispatch(getUser(res.result));
			}
		}
		return res;
	})
		//dirty way 为了避免员工类型是兼职或者实习的时候，状态不是正式.
		// .then(res => {
		// 	if (res.items[1] && res.items[1].customFieldVOList[0].value) {
		// 		const infoObj = res.items[1];
		// 		const value_1 = infoObj.customFieldVOList[0].value
		// 		if (Number(value_1) === 2 || Number(value_1) === 3) {
		// 			infoObj.customFieldVOList[1].value = 3
		// 		}
		// 	}
		// 	return res;
		// });
};

export const saveAll = user => () =>
	fetch(`${get("baseUrl")}updateEmployeeInfoV2`, {
		body: user,
		meta: {
			dipId: -1
		}
	});
